package com.justgame.project.service.system.service.basic;

import com.justgame.project.service.system.entity.SysUserOperationLog;
import com.justgame.project.service.system.query.SysUserOperationLogQuery;
import com.justgame.project.service.system.wrapper.SysUserOperationLogWrapper;
import com.justgame.project.base.servcice.IBasicService;
import java.util.Optional;
import org.springframework.util.Assert;
import java.lang.Long;
import java.time.LocalDateTime;
import org.springframework.lang.NonNull;
import java.lang.String;

/**
 *
 * 用户功能操作间隔限制表服务自定义查询类
 *
 * @@tableName sys_user_operation_log
 * @author Zwb
 */
@SuppressWarnings("all")
public interface BasicSysUserOperationLogService extends IBasicService<SysUserOperationLogQuery,SysUserOperationLog,SysUserOperationLogWrapper> {

   /**
    * 保存操作限制记录
    * @param userInfoId 用户信息
    * @param operation 操作类型
    * @param endDate 结束限制日期
    * @param startDate 操作开始日期
    */
   default SysUserOperationLog saveOperationLog(Long userInfoId, SysUserOperationLog.Operation operation, LocalDateTime endDate){
      Assert.notNull(userInfoId,"必须提供用户信息");
      Assert.notNull(endDate,"必须提供结束限制日期");
      Assert.notNull(operation,"必须提供操作类型");
      SysUserOperationLog sysUserOperationLog = new SysUserOperationLog();
      sysUserOperationLog.setUserInfoId(safeValue(userInfoId, null));
      sysUserOperationLog.setOperation(safeValue(operation, null));
      sysUserOperationLog.setEndDate(safeValue(endDate, null));
      sysUserOperationLog.setStartDate(LocalDateTime.now());
      save(sysUserOperationLog);
      return sysUserOperationLog;
   }

   /**
    * 查询是否被限制操作
    * @param userInfoIdIs 用户信息
    * @param operationIs 操作类型
    * @param endDateGreaterThan 结束限制日期
    */
   default long hasRestrict(Long userInfoIdIs, SysUserOperationLog.Operation operationIs, LocalDateTime endDateGreaterThan){
      Assert.notNull(operationIs,"必须提供操作类型");
      Assert.notNull(endDateGreaterThan,"必须提供结束限制日期");
      Assert.notNull(userInfoIdIs,"必须提供用户信息");
      return count(SysUserOperationLogQuery.builder().getWrapper()
                  .eq(SysUserOperationLog::getUserInfoId,userInfoIdIs)
                  .eq(SysUserOperationLog::getOperation,operationIs)
                  .gt(SysUserOperationLog::getEndDate,endDateGreaterThan));
   }

   /**
    * 查询通过字段：用户信息、操作类型
    * @param userInfoIdIs 用户信息
    * @param operationIs 操作类型
    */
   default Optional<SysUserOperationLog> findByUserInfoIdIsAndOperationIs(Long userInfoIdIs, SysUserOperationLog.Operation operationIs){
      Assert.notNull(operationIs,"必须提供操作类型");
      Assert.notNull(userInfoIdIs,"必须提供用户信息");
      return Optional.ofNullable(getOne(SysUserOperationLogQuery.builder().getWrapper()
                  .eq(SysUserOperationLog::getUserInfoId,userInfoIdIs)
                  .eq(SysUserOperationLog::getOperation,operationIs)));
   }

   /**
    * 更新相关字段：操作开始日期、结束限制日期
    * @param startDate 操作开始日期
    * @param endDate 结束限制日期
    */
   default boolean updateByStartDateAndEndDate(@NonNull SysUserOperationLog sysUserOperationLog,LocalDateTime startDate, LocalDateTime endDate){
      Assert.notNull(endDate,"必须提供结束限制日期");
      Assert.notNull(startDate,"必须提供操作开始日期");
      Assert.notNull(sysUserOperationLog.getId(),"必须提供主键ID");
      sysUserOperationLog.setStartDate(safeValue(startDate, null));
      sysUserOperationLog.setEndDate(safeValue(endDate, null));
      return updateById(sysUserOperationLog);
   }

   /**
    * 新增创建
    * @param userInfoId 用户信息
    * @param operation 操作类型
    * @param startDate 操作开始日期
    * @param endDate 结束限制日期
    */
   default SysUserOperationLog baseAdd(Long userInfoId, SysUserOperationLog.Operation operation, LocalDateTime startDate, LocalDateTime endDate){
      SysUserOperationLog sysUserOperationLog = new SysUserOperationLog();
      sysUserOperationLog.setUserInfoId(safeValue(userInfoId, null));
      sysUserOperationLog.setOperation(safeValue(operation, null));
      sysUserOperationLog.setStartDate(safeValue(startDate, null));
      sysUserOperationLog.setEndDate(safeValue(endDate, null));
      save(sysUserOperationLog);
      return sysUserOperationLog;
   }

   /**
    * 更新
    * @param userInfoId 用户信息
    * @param operation 操作类型
    * @param startDate 操作开始日期
    * @param endDate 结束限制日期
    */
   default boolean baseUpdate(@NonNull SysUserOperationLog sysUserOperationLog,Long userInfoId, SysUserOperationLog.Operation operation, LocalDateTime startDate, LocalDateTime endDate){
      Assert.notNull(userInfoId,"必须提供用户信息");
      Assert.notNull(endDate,"必须提供结束限制日期");
      Assert.notNull(startDate,"必须提供操作开始日期");
      Assert.notNull(operation,"必须提供操作类型");
      Assert.notNull(sysUserOperationLog.getId(),"必须提供主键ID");
      sysUserOperationLog.setUserInfoId(safeValue(userInfoId, null));
      sysUserOperationLog.setOperation(safeValue(operation, null));
      sysUserOperationLog.setStartDate(safeValue(startDate, null));
      sysUserOperationLog.setEndDate(safeValue(endDate, null));
      return updateById(sysUserOperationLog);
   }

}